﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace WorldOnFire
{
    public class CMissile : ABCProjectile
    {
        private Vector3[] m_vTargetPosition;
        private float[] m_fJitterTimer;

        public override void Initialize(int _nObjectCount)
        {
            // Initialize the arrays
            m_vTargetPosition = new Vector3[_nObjectCount];
            m_fJitterTimer = new float[_nObjectCount];

            // Initialize the base members
            base.Initialize(_nObjectCount);

            // Set the enumerations
            eBaseType[] eBaseTypes = new eBaseType[] { eBaseType.MOVING_OBJECT, eBaseType.PROJECTILE };
            this.SetBaseTypeID(eBaseTypes);

            this.SetObjectType(eObjectType.PROJECTILE_MISSILE);
            this.SetProjectileID(eProjectileType.MISSILE);

            // Assign collidable types
            m_CollidableTypeList.Add(eObjectType.TARGET);
        }

        public override void LoadContent()
        {
            m_eModelID = CRenderingManager.eModelID.PROJECTILE_MISSILE;

            base.LoadContent();
        }

        public override void Update(float _fDeltaTime)
        {
            // Turn and accelerate
            for (int iMissile = 0; iMissile < m_nObjectCount; ++iMissile)
            {
                // Set the acceleration
                this.ApplyForce(iMissile, _fDeltaTime * m_mTransform[iMissile].Backward * m_fSpeed[iMissile]);

                // Increment the jitter timer
                m_fJitterTimer[iMissile] -= _fDeltaTime;

                if(m_fJitterTimer[iMissile] < 0.0f)
                {
                    m_fJitterTimer[iMissile] = 0.25f;

                    // Pick a new position to turn towards
                    m_vTargetPosition[iMissile] +=
                        new Vector3(s_Ron.NextFloat(-25.0f, 25.0f),
                            s_Ron.NextFloat(-25.0f, 25.0f),
                            s_Ron.NextFloat(-25.0f, 25.0f));
                }

                // Turn towards the target
                this.TurnTowards(iMissile, m_vTargetPosition[iMissile], _fDeltaTime * 0.05f * (float)Math.PI, false);
            }

            // Update the base members
            base.Update(_fDeltaTime);
        }

        protected override void InitializeProjectile(int _nProjectileIndex, ABCTurret _Turret, int _nTurretIndex, Vector3 _vOffset)
        {
 	        base.InitializeProjectile(_nProjectileIndex, _Turret, _nTurretIndex, _vOffset);

            // Store the target's position
            ABCEnemy Enemy = s_ObjectManager.GetEnemy(_Turret.GetTargetType(_nTurretIndex));
            int nEnemyIndex = _Turret.GetTargetIndex(_nTurretIndex);

            m_vTargetPosition[_nProjectileIndex] = Enemy.GetPosition(nEnemyIndex);

            // Scale down the velocity
            m_vVelocity[_nProjectileIndex] = Vector3.Zero;

            // Factor acceleration into lifetime with this random number
            m_fMaximumTime[_nProjectileIndex] = 5.0f;
        }
    }
}
